package com.lxy.allappinfo;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
	private List<PackageInfo> packages;
	private LayoutInflater inflater;
	private Context context;

	
	public MyAdapter(Context context){
		packages=new ArrayList<PackageInfo>();
		inflater=LayoutInflater.from(context);
		this.context=context;
	}
	
	public void setList(List<PackageInfo> packages){
		this.packages=packages;
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return packages.size();
	}

	@Override
	public Object getItem(int arg0) {
		return packages.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		AppInfo appInfo;
		if(arg1==null){
			appInfo=new AppInfo();
			arg1=inflater.inflate(R.layout.list_item, null);
			appInfo.imv=(ImageView) arg1.findViewById(R.id.imgv);
			appInfo.tv1=(TextView) arg1.findViewById(R.id.tv1);
			appInfo.tv2=(TextView) arg1.findViewById(R.id.tv2);
			appInfo.tv3=(TextView) arg1.findViewById(R.id.tv3);
			appInfo.tv4=(TextView) arg1.findViewById(R.id.tv4);
			arg1.setTag(appInfo);
		}else{
			appInfo=(AppInfo) arg1.getTag();
		}
		PackageInfo pinfo=packages.get(arg0);
		appInfo.imv.setImageDrawable(pinfo.applicationInfo.loadIcon(context.getPackageManager()));
		appInfo.tv1.setText("程序名:"+pinfo.applicationInfo.loadLabel(context.getPackageManager()).toString());
		appInfo.tv2.setText("程序包名:"+pinfo.packageName);
		appInfo.tv3.setText("程序release版本:"+pinfo.versionName);
		appInfo.tv4.setText("程序版本:"+pinfo.versionCode);
		
		return arg1;
	}
	
	static class AppInfo {
		
		ImageView imv;
		TextView tv1;
		TextView tv2;
		TextView tv3;
		TextView tv4;
	}

	
	

}
